package com.sen.common.orm.ibatis;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.sen.common.utils.ReflectUtil;

/**
 * @author sen
 */
public class BaseSqlMapDaoSupport extends SqlMapClientDaoSupport {

	private SqlExecutor sqlExecutor;

	public int getLimitSqlRecordCount() {
		throw new Error("请实现该方法");
	}

	public SqlExecutor getSqlExecutor() {
		return sqlExecutor;
	}

	public void setSqlExecutor(SqlExecutor sqlExecutor) {
		this.sqlExecutor = sqlExecutor;
	}

	public void setEnableLimit(boolean enableLimit) {
		if (sqlExecutor instanceof LimitSqlExecutor) {
			((LimitSqlExecutor) sqlExecutor).setEnableLimit(enableLimit);
		}
	}

	public void setEnableSqlLmitRecordCount(boolean enableSqlLmitRecordCount) {
		if (sqlExecutor instanceof LimitSqlExecutor) {
			((LimitSqlExecutor) sqlExecutor)
					.setEnableSqlLmitRecordCount(enableSqlLmitRecordCount);
		}
	}

	public void initialize() throws Exception {
		if (sqlExecutor == null) {
			return;
		}
		SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
		if (sqlMapClient instanceof ExtendedSqlMapClient) {
			ReflectUtil.setFieldValue(
					((ExtendedSqlMapClient) sqlMapClient).getDelegate(),
					"sqlExecutor", SqlExecutor.class, sqlExecutor);
		}
	}
}
